package com.nimitz.modules.demo.service.impl;


import com.nimitz.common.core.constant.UserConstants;
import com.nimitz.common.core.utils.StringUtils;
import com.nimitz.modules.demo.domain.DemoDepartment;
import com.nimitz.modules.demo.mapper.DemoDepartmentMapper;
import com.nimitz.modules.demo.service.IDemoDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName DemoDepartmentServiceImpl
 * @Author Vincent
 * @Date 2021-08-17-15:34
 * @PackageName com.nimitz.modules.demo.service.impl
 * @Description 演示模块-部门服务层实现类
 * @Version 1.0.0
 */
@Service
public class DemoDepartmentServiceImpl implements IDemoDepartmentService {

    @Autowired
    private DemoDepartmentMapper demoDepartmentMapper;

    /**
     * 查询所有部门信息（含搜索）
     *
     * @param demoDepartment 部门信息
     * @return 部门信息集合
     */
    @Override
    public List<DemoDepartment> selectDemoDepartmentList(DemoDepartment demoDepartment) {
        return demoDepartmentMapper.selectDemoDepartmentList(demoDepartment);
    }

    /**
     * 新增部门信息
     *
     * @param demoDepartment 部门信息
     * @return 结果
     */
    @Override
    public int insertDemoDepartment(DemoDepartment demoDepartment) {
        return demoDepartmentMapper.insertDemoDepartment(demoDepartment);
    }

    /**
     * 修改部门信息
     *
     * @param demoDepartment 部门信息
     * @return 结果
     */
    @Override
    public int updateDemoDepartment(DemoDepartment demoDepartment) {
        return demoDepartmentMapper.updateDemoDepartment(demoDepartment);
    }

    /**
     * 删除部门信息
     * @param demoDepartmentId 部门ID
     * @return 结果
     */
    @Override
    public int deleteDemoDepartmentByDepartmentId(Long demoDepartmentId) {
        return demoDepartmentMapper.deleteDemoDepartmentByDepartmentId(demoDepartmentId);
    }

    /**
     * 校验部门名称是否唯一
     *
     * @param demoDepartment 部门信息
     * @return 结果
     */
    @Override
    public String checkDemoDepartmentNameUnique(DemoDepartment demoDepartment) {
        Long deptId = StringUtils.isNull(demoDepartment.getDepartmentId()) ? -1L : demoDepartment.getDepartmentId();
        DemoDepartment info = demoDepartmentMapper.checkDemoDepartmentNameUnique(demoDepartment.getDepartmentName());
        if (StringUtils.isNotNull(info) && info.getDepartmentId().longValue() != deptId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
